#!/usr/bin/perl
use Getopt::Std;
#use strict;
#getopt("d:a:e:t", \%args);
my %args;
getopt("a:d:t:s", \%args);
my $maindir = $args{d};
my $annotation = $args{a};
my $tpedfile =$args{t};
my $sdrffile = $args{s};
chomp($maindir);
chomp($annotation);
chomp($tpedfile);
chomp($sdrffile);
if($maindir eq "" || $annotation eq "" || $tpedfile  eq "" || $sdrffile  eq "" )
{
	die "no proper arguments:\n";
}
open(TPED,">$tpedfile.tped");
open(TFAM,">$tpedfile.tfam");
open BUFF,"<$annotation" or die "can't open $annotation";
open(SDRF,"$sdrffile");
while($sdf = <SDRF>)
{
	chomp($sdf);
	@array = split(/\t/,$sdf);
	$ss = "birdseed.data.txt";
	$array[1] =~ s/ //g;
	$array[1] =~ s/cel$/$ss/g;	
	$array[1] =~ s/CEL$/$ss/g;
	#print $array[1]."\n";
	$sdrfH{$array[1]} = $array[0];
}
#printing intial lines in the annotation file
for(my $i=0;$i<23;$i++)
{
	my $line = <BUFF>;
	#print $line."\n";
	
}
my $line;
my %annot;
#storinging Allelle1 & 2 to the hash
while($line = <BUFF>)
{
	chomp($line);
	$line =~ s/"//g;
	my @array = split(',',$line);
	chomp($array[0]);
	#print $array[0]."\t".$array[8]."\t".$array[9]."\n";
	if(exists($hash123{$array[1]}))
	{
	}
	else
	{
	$hash123{$array[1]} = 1;
	$annot{$array[0]} = $array[2]."\t".$array[3]."\t".$array[8]."\t".$array[9]."\t".$array[1];
	}
	#if($array[0] eq "SNP_A-2131660")
	#{
	#	print "$array[0]\t$annot{$array[0]}\n";
	#}
}
#opening each file in the directory
open(BUFF,"ls $maindir |");
###@maindirarr = split(/\//,$maindir);

my $line;
###$line = pop(@maindirarr);
###$maindir = join('/',@maindirarr);
my $nu = 1;
#my $test = 0;
#$line = <BUFF>;
#asigning the alleles types according to the bird seed output file description
#${"A0"} = 'AA';
#${"A1"} = 'AB';
#${"A2"} = 'BB';
#print $line."\n";
my %mainhash;

while($line = <BUFF>)
{
        chomp($line);
#	creating tfam file name
#	@tf = split(/\./,$line);
#	@tfa = split('_6_',$tf[0]);
	
	if(exists($sdrfH{$line}))
	{
		$tfam = "$sdrfH{$line} $sdrfH{$line} 0 0 9 -9";	
	}
	else
	{
		die "No sdrf sample name for $line\n";
	}
#	print $tfam."\n";
	print TFAM $tfam."\n";
#creating unique file pointer name to each separate level2 genotype file the given directory
	$BUFF = "BUFF$nu";
	open($BUFF,"$maindir/$line") or die "no file found $maindir/$line\n";
	print "$maindir/$line\n";
	my $liney;
	$liney = <$BUFF>;
	#print $liney."\n";
	$liney = <$BUFF>;
#	$test++;

#}
#print "TEST $test\n";
#open each genotype file

	while($liney=<$BUFF>)
	{
		chomp($liney);
		my @arr =split(/\t/,$liney);
		#print "$arr[0]\n";
		chomp($arr[0]);
		$test = $annot{$arr[0]};
		#print $test."\n";
		if($test ne "")
		{
			my @anno = split("\t",$annot{$arr[0]});
#if bird seed quality is gt 0.1 asign missing
				$A = $anno[2];
				$B = $anno[3];

			my $rsid = $anno[4];
				if(!($rsid =~ m/rs/))
				{
					die "wrong ID $rsid\t$annot{$arr[0]}\n";
				}
				$anno[0] =~ s/X/23/g;
				$anno[0] =~ s/Y/24/g;
				$anno[0] =~ s/MT/26/g;	
			#my @genotype = split('',${"A$arr[1]"});
#concatinating if exists if not create one
				chomp($arr[2]);
				chomp($arr[1]);
				if($arr[2] > 0.1 || $arr[1] eq "-1")
                        	{
                                	$genotype = "0 0";
                        	}
				elsif($arr[1] eq "0")
				{
					$genotype = "$A $A";
				}
				elsif($arr[1] eq "1")
				{
					$genotype = "$A $B";
				}
				elsif($arr[1] eq "2")
                        	{
                                	$genotype = "$B $B";
                        	}
				if(exists($mainhash{$rsid}))
				{
					$mainhash{$rsid} =$mainhash{$rsid}." ".$genotype;
				}
				else
				{
				
					$mainhash{$rsid} = $anno[0]." ".$rsid." "."0 ".$anno[1]." ".$genotype;
				}
		}
		else
		{
			#print "something wrong with this snp id $line\t$liney1\n";
		}
		
	}
	$nu++;
	close($BUFF);	
}
#print tped file
while(my($key,$value) = each %mainhash)
{
	print TPED $value."\n";
}
